/*******************************************************************************
Title: Individualism during Crisis 
Authors: Bo Bian, Jingjing Li, Ting Xu, Natasha Z. Foutz
Purpose: Produce the following tables and figures
         - Table 3
		 - Appendix Table A1, A5, A8
		 - Figure 2
		 - Appendix Figure A2(a) and A2(b)
		 
Please note, due to the NDA agreement with Xmode we are not able to provide disaggregated individual-level dataset based on which these the above tables are generated. The below analysis code for these figures and tables is provided for reference.
*******************************************************************************/

clear all
set more off
set matsize 5000
cap log close
set linesize 200

log using "tables\rep_individual_level", replace
use "data\rep_individual_level_sample.dta", clear

******************************************************************************** 
* Table 3
********************************************************************************
est clear
eststo T3A1: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin, ab(id date) cl(fips) 
eststo T3A2: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin if all_records_in_day>=48, ab(id date) cl(fips) 
eststo T3A3: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin if (stayer==1 | mover_in_state==1), ab(id date) cl(fips)
eststo T3A4: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin if (stayer==1 | mover_out_state==1), ab(id date) cl(fips)
esttab T3A*, ar2 star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) br mtitle title(Table 3 Panel A; fe: individual, date) keep(1.post*#* 1.post*) order (*tfe_bin* *post*) replace

eststo T3B1: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin, ab(id date#city_state_id) cl(fips) 
eststo T3B2: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin if all_records_in_day>=48, ab(id date#city_state_id) cl(fips) 
eststo T3B3: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin if (stayer==1 | mover_in_state==1), ab(id date#city_state_id) cl(fips)
eststo T3B4: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin if (stayer==1 | mover_out_state==1), ab(id date#city_state_id) cl(fips)
esttab T3B*, ar2 star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) br mtitle title(Table 3 Panel B; fe: individual, city x date) keep(1.post*#*) replace

eststo T3C1: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin [aw=all_records_in_day], ab(id date#city_state_id) cl(fips) 
eststo T3C2: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin [aw=all_records_in_day] if all_records_in_day>=48, ab(id date#city_state_id) cl(fips) 
eststo T3C3: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin [aw=all_records_in_day] if (stayer==1 | mover_in_state==1), ab(id date#city_state_id) cl(fips)
eststo T3C4: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin [aw=all_records_in_day] if (stayer==1 | mover_out_state==1), ab(id date#city_state_id) cl(fips)
esttab T3B*, ar2 star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) br mtitle title(Table 3 Panel C; fe: individual, city x date; weighted by tracking freq) keep(1.post*#*) order (*tfe_bin*) replace

******************************************************************************** 
* Table A1 (summary stat), Figure 2(b), Figure 2(c)
********************************************************************************
preserve 
	* collpase data to individual-city level 
	collapse (max)stayer mover_in_state mover_out_state (sum)all_records=all_records_in_day (count)N_day=date (mean)pct_shelter all_records_in_day, by (city id fips_old)
	quietly format N_day %5.0g
	quietly isid id, s
	quietly egen N_county=nvals(fips_old), by (city) /* number of origin counties */
	save "data\rep_individual_level_temp.dta", replace
	
	* collapse data to city level (12 cities)
	collapse (sum)stayer mover_in_state mover_out_state (max)N_county (mean)all_records_in_day pct_shelter, by (city)
	
	* generate variables to summarize
	g share_migration=100*(mover_in_state+mover_out_state)/(stayer+mover_in_state+mover_out_state)
	g share_in_state=100*(mover_in_state)/(stayer+mover_in_state+mover_out_state)
	g share_out_state=100*(mover_out_state)/(stayer+mover_in_state+mover_out_state)
	g N_all=stayer+mover_in_state+mover_out_state
	
	* Table A1: summary statistics (summarized across 12 cities)
	tabstat N_all share_migration share_in_state share_out_state N_county all_records_in_day, stat(N mean sd min p50 max) columns(s) varwidth(20) format(%9.3gc)

	* Figure 2(c): number of individuals and number of origin counties 
	replace city="New York" if city=="New_York"
	replace city="SF" if city=="SanFran_Oakland"
	
	generate pos = 3
	replace pos = 9 if city=="DC"
	scatter N_all N_county, mlabel(city) mlabsize(tiny) mlabv(pos) ///
	xtitle("Number of Origin Counties", size(small)) xlabel(, labsize(small)) ///
	ytitle("Number of Individuals", size(small)) ylabel(, labsize(small)) ///
	graphregion(color(white)) scheme(s2mono)
	graph export "figures\F2c.pdf", replace	
	graph export "figures_png\F2c.png", replace
	
	* Figure 2(b): percentage of migrants (in-state and out-of-state)
	keep city N_county N_all share_in_state share_out_state pct_shelter
	ren share_in_state share0
	ren share_out_state share1
	reshape long share, i(city N_county N_all pct_shelter) j(new)
	ren new inout
	g in_out_string="% In-State" if inout==0
	replace in_out_string="% Out-of-State" if inout==1
	egen share_total=sum(share), by (city)
	graph bar (mean) share, over(in_out_string) over(city, sort(share_total) descending label(ticks labs(vsmall))) asyvars stack graphregion(color(white)) ///
	ytitle("% Migrants (In-State and Out-of-State)", size(small)) ylabel(, labsize(small)) ///
	blabel(bar, position(center) format(%9.2f) size(2) color(white)) ///
	legend(size(2) rowgap(*.45) pos(6) rows (1) region(style(legend) fcolor(gs15) margin(medsmall)) colgap(*.75) symxsize(*.75) keygap(*.33)) scheme(s2mono)
	graph export "figures\F2b.pdf", replace
	graph export "figures_png\F2b.png", replace
restore


******************************************************************************** 
* Table A5
********************************************************************************
est clear
label var N_days_visit "Number of days visiting the orgin county"
label var L_migrant "Time since migration"
eststo TA5A1: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin if  (stayer==1 | (N_days_visit>0 & N_days_visit<.)), ab(id date) cl(fips) 
eststo TA5A2: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin if  (stayer==1 | N_days_visit==0), ab(id date) cl(fips) 
eststo TA5A3: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin if  (stayer==1 | L_migrant<=3), ab(id date) cl(fips) 
eststo TA5A4: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin if  (stayer==1 | (L_migrant>3 & L_migrant<=15)), ab(id date) cl(fips) 
esttab TA5A*, ar2 star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) br mtitle title(Table A5 Panel A; fe: individual, date) keep(1.post*#*) replace

eststo TA5B1: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin if  (stayer==1 | (N_days_visit>0 & N_days_visit<.)), ab(id date#city_state_id) cl(fips) 
eststo TA5B2: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin if  (stayer==1 | N_days_visit==0), ab(id date#city_state_id) cl(fips) 
eststo TA5B3: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin if  (stayer==1 | L_migrant<=3), ab(id date#city_state_id) cl(fips) 
eststo TA5B4: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin if  (stayer==1 | (L_migrant>3 & L_migrant<=15)), ab(id date#city_state_id) cl(fips) 
esttab TA5B*, ar2 star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) br mtitle title(Table A5 Panel B; fe: individual, city x date) keep(1.post*#*) replace

eststo TA5C1: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin [aw=all_records_in_day] if  (stayer==1 | (N_days_visit>0 & N_days_visit<.)), ab(id date#city_state_id) cl(fips) 
eststo TA5C2: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin [aw=all_records_in_day] if  (stayer==1 | N_days_visit==0), ab(id date#city_state_id) cl(fips) 
eststo TA5C3: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin [aw=all_records_in_day] if  (stayer==1 | L_migrant<=3), ab(id date#city_state_id) cl(fips) 
eststo TA5C4: quietly reghdfe pct_shelter post_lockdown##c.tfe_bin [aw=all_records_in_day] if  (stayer==1 | (L_migrant>3 & L_migrant<=15)), ab(id date#city_state_id) cl(fips) 
esttab TA5C*, ar2 star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) br mtitle title(Table A5 Panel C; fe: individual, city x date) keep(1.post*#*) replace
				
******************************************************************************** 
* Table A8 
********************************************************************************
est clear	
eststo A8_1: quietly reghdfe pct_shelter post_lockdown##c.infreq_name, ab(id date) cl(fips) 
eststo A8_2: quietly reghdfe pct_shelter post_lockdown##c.nonpatmat_name, ab(id date) cl(fips) 
eststo A8_3: quietly reghdfe pct_shelter post_lockdown##c.infreq_name, ab(id date#city_state_id) cl(fips) 
eststo A8_4: quietly reghdfe pct_shelter post_lockdown##c.nonpatmat_name, ab(id date#city_state_id) cl(fips) 
esttab A8*, ar2 star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) br mtitle title(Table A8) keep(1.post*#*infreq* 1.post*#*patmat* 1.post*) order(1.post*#*infreq* 1.post*#*patmat* 1.post*) replace

******************************************************************************** 
* Figure 2(a)
********************************************************************************
cd "data\us_county_shape"
use "..\rep_individual_level_temp.dta", replace
keep if stayer==0
collapse (count)N=stayer, by (fips_old)
ren fips_old fips
save "..\figure_2a_temp1.dta", replace

use "..\rep_individual_level_temp.dta", clear 
tab city 
keep if stayer==1	
collapse (count)N=stayer, by (fips_old city)
ren fips_old fips
egen max=max(N), by (city)
keep if max==N
keep fips city 
save "..\figure_2a_temp2.dta", replace
use _ID _CX _CY GEOID STATEFP fips using "usacounties.dta", clear
merge 1:m fips using "..\figure_2a_temp2.dta"
keep if _m==3
drop _m
ren _CX x_Centro
ren _CY y_Centro
save "..\figure_2a_temp3.dta", replace
erase "..\figure_2a_temp2.dta"

use _ID _CX _CY GEOID STATEFP fips using "usacounties.dta", clear
merge 1:m fips using "..\figure_2a_temp1.dta"
erase "..\figure_2a_temp1.dta"
drop if _m==2
drop _m

xtset, clear
grmap, activate
drop if inlist(STATEFP, "02", "15", "43", "52", "60", "66", "69", "72", "78")
spset, modify shpfile(usacounties_shp)

grmap N, clnumber(15) ndocolor(gs10) ndsize(vvthin) fcolor(Greys2) ocolor(gs10 ...) osize(vvthin ...) ///
point(data(..\figure_2a_temp3.dta) ///
x(x_Centro) y(y_Centro) size(small) fc(white) oc(gray))
graph export ..\..\figures\F2a.pdf, replace	
graph export "..\..\figures_png\F2a.png", replace
erase "..\figure_2a_temp3.dta"

******************************************************************************** 
* Figure A2(a), A2(b)
********************************************************************************
global city "New_York Seattle"
foreach x in $city {
	use "..\rep_individual_level_temp.dta", clear 
	keep if stayer==0
	keep if city=="`x'"
	collapse (count)N=stayer, by (fips_old)
	ren fips_old fips
	save "..\figure_A2_temp1.dta", replace
	
	use "..\rep_individual_level_temp.dta", clear 
	keep if city=="`x'"
	keep if stayer==1	
	collapse (count)N=stayer, by (fips_old city)
	ren fips_old fips
	egen max=max(N), by (city)
	keep if max==N
	keep fips city 
	save "..\figure_A2_temp2.dta", replace
	use _ID _CX _CY GEOID STATEFP fips using "usacounties.dta", clear
	merge 1:m fips using "..\figure_A2_temp2.dta"
	keep if _m==3
	drop _m
	ren _CX x_Centro
	ren _CY y_Centro
	save "..\figure_A2_temp3.dta", replace
	erase "..\figure_A2_temp2.dta"
		
	use _ID _CX _CY GEOID STATEFP fips using  "usacounties.dta", clear
	merge 1:m fips using "..\figure_A2_temp1.dta"
	erase "..\figure_A2_temp1.dta"
	drop if _m==2
	drop _m

	xtset, clear
	grmap, activate
	drop if inlist(STATEFP, "02", "15", "43", "52", "60", "66", "69", "72", "78")
	spset, modify shpfile(usacounties_shp)

	grmap N, clnumber(15) ndocolor(gs10) ndsize(vvthin) fcolor(Greys2) ocolor(gs10 ...) osize(vvthin ...) ///
	point(data(..\figure_A2_temp3.dta) ///
	x(x_Centro) y(y_Centro) size(small) fc(white) oc(gray))

	graph export ..\..\figures\AF2_`x'.pdf, replace	
	graph export "..\..\figures_png\AF2_`x'.png", replace
	erase "..\figure_A2_temp3.dta"
}
erase "..\rep_individual_level_temp.dta"
cd ..
cd ..


log close